<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="生产线编码" prop="proLineCode">
        <el-input
          v-model="queryParams.proLineCode"
          placeholder="请输入生产线编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产线名称" prop="proLineName">
        <el-input
          v-model="queryParams.proLineName"
          placeholder="请输入生产线名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工位编码" prop="stationCode">
        <el-input
          v-model="queryParams.stationCode"
          placeholder="请输入工位编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工位名称" prop="stationName">
        <el-input
          v-model="queryParams.stationName"
          placeholder="请输入工位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="工位顺序" prop="stationSort">-->
<!--        <el-input-->
<!--          v-model="queryParams.stationSort"-->
<!--          placeholder="请输入工位顺序"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="生产节拍" prop="proRhythm">-->
<!--        <el-input-->
<!--          v-model="queryParams.proRhythm"-->
<!--          placeholder="请输入生产节拍"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="设备名称" prop="devName">-->
<!--        <el-input-->
<!--          v-model="queryParams.devName"-->
<!--          placeholder="请输入设备名称"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="设备编号" prop="devCode">-->
<!--        <el-input-->
<!--          v-model="queryParams.devCode"-->
<!--          placeholder="请输入设备编号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="设备功率" prop="devPower">-->
<!--        <el-input-->
<!--          v-model="queryParams.devPower"-->
<!--          placeholder="请输入设备功率"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="设备产能" prop="devCapacity">-->
<!--        <el-input-->
<!--          v-model="queryParams.devCapacity"-->
<!--          placeholder="请输入设备产能"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="切削工艺" prop="cutProcess">-->
<!--        <el-input-->
<!--          v-model="queryParams.cutProcess"-->
<!--          placeholder="请输入切削工艺"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="上工位" prop="upSta">-->
<!--        <el-input-->
<!--          v-model="queryParams.upSta"-->
<!--          placeholder="请输入上工位"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="下工位" prop="downSta">-->
<!--        <el-input-->
<!--          v-model="queryParams.downSta"-->
<!--          placeholder="请输入下工位"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="是否运行" prop="isRunning">-->
<!--        <el-input-->
<!--          v-model="queryParams.isRunning"-->
<!--          placeholder="请输入是否运行"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['technology:processTec:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['technology:processTec:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['technology:processTec:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['technology:processTec:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="processTecList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公司代码" align="center" prop="factoryCode" />
      <el-table-column label="公司名称" align="center" prop="factoryName" />
      <el-table-column label="生产线编码" width="120" align="center" prop="proLineCode" />
      <el-table-column label="生产线名称" width="150" align="center" prop="proLineName" />
      <el-table-column label="工位编码" width="120" align="center" prop="stationCode" />
      <el-table-column label="工位名称" width="150" align="center" prop="stationName" />

<!--      <el-table-column label="设备名称" align="center" prop="devName" />-->
<!--      <el-table-column label="设备编号" align="center" prop="devCode" />-->
<!--      <el-table-column label="设备功率" align="center" prop="devPower" />-->
<!--      <el-table-column label="设备产能" align="center" prop="devCapacity" />-->
      <el-table-column label="派工工位编码" width="130" align="center" prop="pgGwCode" />
      <el-table-column label="派工工位名称" width="150" show-overflow-tooltip align="center" prop="pgGwName" />
      <el-table-column label="派工工位负责人" width="150" show-overflow-tooltip align="center" prop="pgGwFzr2" />
      <el-table-column label="工位人员数量" width="100" align="center" prop="gwPersonNum" />
      <el-table-column label="派工工位操作人员" width="150" show-overflow-tooltip align="center" prop="gwPersonInfo2" />
      <el-table-column label="工位顺序" align="center" prop="stationSort" />
      <el-table-column label="切削工艺" align="center" prop="cutProcess" />
      <el-table-column label="生产节拍" align="center" prop="proRhythm" />
      <el-table-column label="上工位" align="center" prop="upSta" />
      <el-table-column label="下工位" align="center" prop="downSta" />
      <el-table-column label="是否运行" align="center" prop="isRunning" />
      <el-table-column label="操作" width="140" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['technology:processTec:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['technology:processTec:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工序工艺对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="生产线编码" prop="proLineCode">
          <el-input v-model="form.proLineCode" placeholder="请输入生产线编码" />
        </el-form-item>
        <el-form-item label="生产线名称" prop="proLineName">
          <el-input v-model="form.proLineName" placeholder="请输入生产线名称" />
        </el-form-item>
        <el-form-item label="工位编码" prop="stationCode">
          <el-input v-model="form.stationCode" placeholder="请输入工位编码" />
        </el-form-item>
        <el-form-item label="工位名称" prop="stationName">
          <el-input v-model="form.stationName" placeholder="请输入工位名称" />
        </el-form-item>
        <el-form-item label="工位顺序" prop="stationSort">
          <el-input v-model="form.stationSort" placeholder="请输入工位顺序" />
        </el-form-item>
        <el-form-item label="生产节拍" prop="proRhythm">
          <el-input v-model="form.proRhythm" placeholder="请输入生产节拍" />
        </el-form-item>
        <el-form-item label="派工工位名称" prop="pgGwName">
          <el-input v-model="form.pgGwName" placeholder="请输入派工工位名称" />
        </el-form-item>
        <el-form-item label="派工工位负责人" prop="pgGwFzr">
          <el-select @change="getpgGwFzr" v-model="form.pgGwFzr" filterable placeholder="请选择派工工位负责人" clearable>
            <el-option
              v-for="dict in userList"
              :key="dict.userId"
              :label="dict.nickName"
              :value="dict.userId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="派工工位操作人员" prop="gwPersonInfo">
          <el-select @change="getGwPersonInfo"  v-model="form.gwPersonInfo" multiple filterable placeholder="请选择派工工位操作人员" clearable>
            <el-option
              v-for="dict in userList"
              :key="dict.userId"
              :label="dict.nickName"
              :value="dict.userId"
            />
          </el-select>
        </el-form-item>
<!--        <el-form-item label="设备名称" prop="devName">-->
<!--          <el-input v-model="form.devName" placeholder="请输入设备名称" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="设备编号" prop="devCode">-->
<!--          <el-input v-model="form.devCode" placeholder="请输入设备编号" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="设备功率" prop="devPower">-->
<!--          <el-input v-model="form.devPower" placeholder="请输入设备功率" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="设备产能" prop="devCapacity">-->
<!--          <el-input v-model="form.devCapacity" placeholder="请输入设备产能" />-->
<!--        </el-form-item>-->
        <el-form-item label="切削工艺" prop="cutProcess">
          <el-input v-model="form.cutProcess" placeholder="请输入切削工艺" />
        </el-form-item>
        <el-form-item label="上工位" prop="upSta">
          <el-input v-model="form.upSta" placeholder="请输入上工位" />
        </el-form-item>
        <el-form-item label="下工位" prop="downSta">
          <el-input v-model="form.downSta" placeholder="请输入下工位" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :close-on-click-modal="false" :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!--  <div class="el-upload__tip" slot="tip">
              <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
            </div>-->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProcessTec, getProcessTec, delProcessTec, addProcessTec, updateProcessTec } from "@/api/technology/processTec";
import {getToken} from "@/utils/auth";
import {userDictList} from "@/api/system/user";

export default {
  name: "ProcessTec",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工序工艺表格数据
      processTecList: [],
      userList:[],
      //操作人员
      lableNames:[],
      jobNoLableNames:[],
      jobNos:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        factoryCode: null,
        factoryName: null,
        proLineCode: null,
        proLineName: null,
        stationCode: null,
        stationName: null,
        stationSort: null,
        proRhythm: null,
        devName: null,
        devCode: null,
        devPower: null,
        devCapacity: null,
        cutProcess: null,
        upSta: null,
        downSta: null,
        isRunning: null
      },
      // 表单参数
      form: {},

      upload: {
        // 是否显示弹出层（物料导入）
        open: false,
        // 弹出层标题（物料导入）
        title: "导入物料",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/technology/processTec/importData"
      },
      // 表单校验
      rules: {
        pgGwFzr: [
          { required: true, message: "请选择派工工位负责人", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    userDictList().then(response => {
      response.data.map(item=>{
        item.userId = String(item.userId)
      })
      this.userList = response.data;
    });
  },
  methods: {
    /** 查询工序工艺列表 */
    getList() {
      this.loading = true;
      listProcessTec(this.queryParams).then(response => {
        this.processTecList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        factoryCode: null,
        factoryName: null,
        proLineCode: null,
        proLineName: null,
        stationCode: null,
        stationName: null,
        stationSort: null,
        proRhythm: null,
        devName: null,
        devCode: null,
        devPower: null,
        devCapacity: null,
        cutProcess: null,
        upSta: null,
        downSta: null,
        isRunning: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    getpgGwFzr(value){
      let selectedName = {};
      selectedName = this.userList.find((item)=>{
        return item.userId === value;
        //筛选出匹配数据，是对应数据的整个对象
      });
      this.form.pgGwFzr2=selectedName.nickName;
      this.form.pgGwFzr1=selectedName.jobNo;
    },
    getGwPersonInfo(value){
      this.lableNames= [];
      this.jobNos= [];
      let selectedName = {};
      for(let i=0;i<=value.length-1;i++){
        selectedName = this.userList.find((item)=>{
          if(item.userId == value[i]){
            this.lableNames.push(item.nickName);
            this.jobNos.push(item.jobNo);
          }
        });
      }
      this.form.gwPersonInfo1=this.jobNos.toString();
      this.form.gwPersonInfo2=this.lableNames.toString();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工序工艺";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProcessTec(id).then(response => {
        this.form = response.data;
        if(this.form.gwPersonInfo!=null && this.form.gwPersonInfo!=""){
          this.form.gwPersonInfo=this.form.gwPersonInfo.split(',');
        }
        this.open = true;
        this.title = "修改工序工艺";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.form.gwPersonInfo= this.form.gwPersonInfo.toString();
            updateProcessTec(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.gwPersonInfo= this.form.gwPersonInfo.toString();
            addProcessTec(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除工序工艺编号为"' + ids + '"的数据项？').then(function() {
        return delProcessTec(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('technology/processTec/export', {
        ...this.queryParams
      }, `processTec_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "工位导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('technology/processTec/importTemplate', {
      }, `processTec_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
  }
};
</script>
